﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Commands.Data;
using ZLCMS.Commands.Member.Commands.Product;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using System.Text;

namespace ZLCMS.Web.Wap.Member
{
    public partial class LoginSuccess : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断token是否有效
            string token = Request["token"];
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                string acc = session.CreateSQLQuery("select account_id from member_info where account_id=:acc")
                    .SetParameter("acc", token)
                    .UniqueResult<string>();
                if (acc != token)
                {
                    Server.Transfer("login.aspx");
                }

                string Origin = Request["origin"];
                if (string.IsNullOrEmpty(Origin) && HttpContext.Current.Request.Cookies["origin"] != null)
                {
                    Origin = HttpContext.Current.Request.Cookies["origin"].Value as string;
                }
                //插入登录记录
                session.CreateSQLQuery("insert into member_login_log(member_account,login_time,Origin) values(:acc,:date,:Origin)")
                    .SetParameter("acc", token)
                    .SetParameter("date", DateTime.Now)
                     .SetParameter("Origin", Origin)
                    .ExecuteUpdate();
                if (!string.IsNullOrEmpty(Origin))
                {
                    HttpCookie orCookie = new HttpCookie("origin", HttpUtility.UrlEncode(Origin, Encoding.GetEncoding("UTF-8")));
                    HttpContext.Current.Response.Cookies.Add(orCookie);
                }

                //string appToken = session.CreateSQLQuery("select app_account from member_bind_info where member_account=:acc and status=1")
                //       .SetParameter("acc", token)
                //       .UniqueResult<string>();
                //if (!string.IsNullOrEmpty(appToken))
                //{
                //    token = appToken;
                //}
            }

            //设置session
            Session[ZLCMS.Business.Util.GlobalSessionString.CurrentMemberAccount] = token;

            //合并购物车数据
            string selectProducts = "";
            if (HttpContext.Current.Request.Cookies["visit_select_product"] != null)
            {
                selectProducts = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["visit_select_product"].Value, Encoding.GetEncoding("UTF-8"));
            }
            if (!string.IsNullOrEmpty(selectProducts))
            {
                List<SelectProductParameterData> selectProductList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SelectProductParameterData>>(selectProducts);
                new SelectProductCommand().Execute(new SelectProductParameter() { MemberAccount = Request["token"], ProductList = selectProductList });

                HttpContext.Current.Response.Cookies.Add(new HttpCookie("visit_select_product", ""));
            }
            
            //string url = Request["goto"];
            //if (!string.IsNullOrEmpty(url))
            //{
            //    Server.Transfer(url);
            //    //url = "/wap/shop/";
            //}
        }
    }
}